package cn.mcch.investigator.viewdelegate;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.mcch.investigator.customview.TitleBar;
import cn.mcch.investigator.listener.OnCommonClickListener;
import cn.mcch.investigator.utils.DensityUtils;
import cn.mcch.investigator.utils.StatusBarUtils;

/**
 * view代理基类
 *
 * @author Administrator
 */
public abstract class BaseViewDelegate {

    private LinearLayout root;
    private FrameLayout contentFrameLayout;

    /**
     * 正在加载数据展示的view
     */
    private View loadingView;

    /**
     * 加载数据失败展示的view
     */
    private View loadErrorView;

    /**
     * 加载数据成功要展示的view
     */

    private View loadSuccessView;
    /**
     * 加载数据成功但是数据为空要展示的view
     */
    private View loadEmptyView;

    /**
     * 保存子view的集合
     */
    private SparseArray<View> mViewsArray = new SparseArray<View>();

    /**
     * titlebar
     */
    private TitleBar mTitleBar;

    /**
     * 创建根布局
     */
    public void createRootView(Context context) {
        root = new LinearLayout(context);
        FrameLayout.LayoutParams rootLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        root.setLayoutParams(rootLayoutParams);
        root.setOrientation(LinearLayout.VERTICAL);

    }

    public View getRootView() {
        return root;
    }

    /**
     * 创建初始化界面要展示的第一个view
     *
     * @param firstLayoutId
     * @param l
     */
    public void createFirstView(Context context, int firstLayoutId, OnCommonClickListener l) {

        if (loadingView == null) {
            loadingView = View.inflate(context, firstLayoutId, null);
        }
        addView(loadingView);
        initEventsListener(l);
    }

    /**
     * @param context
     * @param l
     */
    public void createLoadErrorView(Context context, OnCommonClickListener l) {
        if (loadErrorView == null) {
            loadErrorView = View.inflate(context,
                    context.getResources().getIdentifier("layout_load_error", "layout", context.getPackageName()),
                    null);
        }
        addView(loadErrorView);
        initLoadErrorViewEventsListener(context, l);
    }

    /**
     * 创建加载数据为空的view
     *
     * @param l
     */
    public void createLoadEmptyView(Context context, OnCommonClickListener l) {
        if (loadEmptyView == null) {
            loadEmptyView = View.inflate(context,
                    context.getResources().getIdentifier("layout_load_empty", "layout", context.getPackageName()),
                    null);
        }
        addView(loadEmptyView);
        initLoadErrorViewEventsListener(context, l);
    }

    /**
     * @param context
     * @param loadSuccessLayoutId
     * @param l
     */
    public void createSuccessView(Context context, int loadSuccessLayoutId, OnCommonClickListener l) {
        if (loadSuccessView == null) {
            loadSuccessView = View.inflate(context, loadSuccessLayoutId, null);
        }
        addView(loadSuccessView);
        initEventsListener(l);
    }

    /**
     * 展示load success view所需要设置的监听事件
     *
     * @param l
     */
    public abstract void initEventsListener(OnCommonClickListener l);

    /**
     * 展示load error view所需要设置的监听事件
     *
     * @param context
     * @param l
     */
    public void initLoadErrorViewEventsListener(Context context, OnCommonClickListener l) {
        setOnClickListener(l, context.getResources().getIdentifier("bt_load_again", "id", context.getPackageName()));
    }

    /**
     * @param context
     * @param title
     * @param ivLeftId
     * @param ivRightId
     * @param bgColorId
     * @param l
     */
    @SuppressWarnings("deprecation")
    public void initTitleBar(boolean isTranslucentStatus, Context context, String title, int ivLeftId, int ivRightId, int bgColorId,
                             OnCommonClickListener l) {

        mTitleBar = new TitleBar(context);
        LinearLayout.LayoutParams titleBarLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(context, 50));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isTranslucentStatus) {
            titleBarLayoutParams.topMargin = StatusBarUtils.getStatusBarHeight(context);
        }
        mTitleBar.setLayoutParams(titleBarLayoutParams);
        root.addView(mTitleBar);
        mTitleBar.initTitleBar(title, ivLeftId, ivRightId, l);
        root.setBackgroundColor(context.getResources().getColor(bgColorId));

    }

    /**
     * 设置titlebar标题
     *
     * @param title
     */
    public void setTitleBarText(String title) {
        mTitleBar.setTitleBarText(title);
    }

    /**
     * 初始化内容区域
     */
    public void initConetentView(Context context) {
        contentFrameLayout = new FrameLayout(context);
        LinearLayout.LayoutParams contentFrameLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        contentFrameLayout.setLayoutParams(contentFrameLayoutParams);
        contentFrameLayout.setBackgroundColor(Color.WHITE);
        root.addView(contentFrameLayout);
    }

    /**
     * @param view
     */
    private void addView(View view) {
        contentFrameLayout.removeAllViews();
        mViewsArray.clear();
        contentFrameLayout.addView(view);
    }

    /**
     * 获取指定id的view
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T findViewById(int id) {
        T view = (T) mViewsArray.get(id);
        if (view == null) {
            view = (T) contentFrameLayout.findViewById(id);
            mViewsArray.put(id, view);
        }
        return view;
    }

    /**
     * 设置view可见性
     *
     * @param id
     * @param visibility
     * @return
     */
    public BaseViewDelegate setVisibility(int id, boolean visibility) {
        findViewById(id).setVisibility(visibility ? View.VISIBLE : View.GONE);
        return this;

    }
    /**
     * 设置view是否可用
     *
     * @param id
     * @param isEnable
     * @return
     */
    public BaseViewDelegate setEnable(int id, boolean isEnable) {
        findViewById(id).setEnabled(isEnable);
        return this;

    }

    /**
     * @param id
     * @param alpha
     * @return
     */
    public BaseViewDelegate setAlpha(int id, float alpha) {
        findViewById(id).setAlpha(alpha);
        return this;

    }

    /**
     * @param id
     * @param checked
     * @return
     */
    public BaseViewDelegate setChecked(int id, boolean checked) {
        ((CheckBox) findViewById(id)).setChecked(checked);
        return this;

    }

    /**
     * 设置AdapterView的适配器
     *
     * @param adapterViewId
     * @param adapter
     */
    @SuppressWarnings("unchecked")
    public BaseViewDelegate setAdapterViewAdapter(int adapterViewId, BaseAdapter
            adapter) {
        ((AdapterView<BaseAdapter>)
                findViewById(adapterViewId)).setAdapter(adapter);
        return this;
    }

    /**
     * 设置viewpager适配器
     *
     * @param viewPagerId
     * @param adapter
     */
    public BaseViewDelegate setViewPagerAdapter(int viewPagerId, PagerAdapter
            adapter) {
        ((ViewPager) findViewById(viewPagerId)).setAdapter(adapter);
        return this;
    }

    /**
     * 获取view的文本信息
     *
     * @param id
     * @return
     */
    public String getText(int id) {
        return ((TextView) findViewById(id)).getText().toString().trim();
    }

    /**
     * 设置view的文本信息
     *
     * @param id
     * @param content
     * @return
     */
    public BaseViewDelegate setText(int id, CharSequence content) {
        ((TextView) findViewById(id)).setText(content);
        return this;

    }

    /**
     * 设置view的文本颜色
     *
     * @param id
     * @param color
     * @return
     */
    public BaseViewDelegate setTextColor(int id, int color) {
        ((TextView) findViewById(id)).setTextColor(color);
        return this;

    }

    /**
     * 设置view的文字大小
     *
     * @param id
     * @param size
     * @return
     */
    public BaseViewDelegate setTextSize(int id, float size) {
        ((TextView) findViewById(id)).setTextSize(size);
        return this;
    }

    /**
     * 设置imageview图片
     *
     * @param viewId
     * @param bm
     * @return
     */
    public BaseViewDelegate setImageBitmap(int viewId, Bitmap bm) {
        ((ImageView) findViewById(viewId)).setImageBitmap(bm);
        return this;
    }

    /**
     * 设置imageview图片
     *
     * @param viewId
     * @param drawable
     * @return
     */
    public BaseViewDelegate setImageDrawable(int viewId, Drawable drawable) {
        ((ImageView) findViewById(viewId)).setImageDrawable(drawable);
        return this;
    }

    /**
     * 设置imageview图片
     *
     * @param viewId
     * @param resId
     * @return
     */
    public BaseViewDelegate setImageResource(int viewId, int resId) {
        ((ImageView) findViewById(viewId)).setImageResource(resId);
        return this;
    }

    /**
     * 设置imageview图片
     *
     * @param viewId
     * @param uri
     * @return
     */
    public BaseViewDelegate setImageURI(int viewId, Uri uri) {
        ((ImageView) findViewById(viewId)).setImageURI(uri);
        return this;
    }

    /**
     * 设置View背景
     *
     * @param viewId
     * @param resId
     * @return
     */
    public BaseViewDelegate setBackgroundResource(int viewId, int resId) {
        ((View) findViewById(viewId)).setBackgroundResource(resId);
        return this;
    }

    /**
     * @param viewId
     * @param color
     * @return
     */
    public BaseViewDelegate setBackgroundColor(int viewId, int color) {
        ((View) findViewById(viewId)).setBackgroundColor(color);
        return this;
    }

    /**
     * 设置view的点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    public BaseViewDelegate setOnClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }

        for (int id : ids) {
            View view = findViewById(id);
            if (view != null) {
                view.setOnClickListener(l);
            }
        }
        return this;
    }

    /**
     * 设置view的长按点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    public BaseViewDelegate setOnLongClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            View view = findViewById(id);
            if (view != null) {
                view.setOnLongClickListener(l);
            }
        }
        return this;
    }


    /**
     * 设置AdapterView的条目点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    @SuppressWarnings("rawtypes")
    public BaseViewDelegate setOnItemClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            AdapterView adapterView = (AdapterView) findViewById(id);
            if (adapterView != null) {
                adapterView.setOnItemClickListener(l);
            }
        }
        return this;
    }

    /**
     * 设置vie的条目长按点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    @SuppressWarnings("rawtypes")
    public BaseViewDelegate setOnItemLongClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            AdapterView adapterView = (AdapterView) findViewById(id);
            if (adapterView != null)
                adapterView.setOnItemLongClickListener(l);
        }
        return this;
    }

    /**
     * 设置viewpager的OnPageChangeListener
     *
     * @param l
     * @param ids
     * @return
     */
    @SuppressWarnings("rawtypes")
    public BaseViewDelegate addOnPageChangeListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            ViewPager adapterView = (ViewPager) findViewById(id);
            if (adapterView != null)
                adapterView.addOnPageChangeListener(l);
        }
        return this;
    }

    /**
     * 设置viewpager的OnPageChangeListener
     *
     * @param l
     * @param ids
     * @return
     */
    @SuppressWarnings("rawtypes")
    public BaseViewDelegate setOnChildClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            ExpandableListView expandableListView = (ExpandableListView) findViewById(id);
            if (expandableListView != null)
                expandableListView.setOnChildClickListener(l);
        }
        return this;
    }

}